//
//  HJNetworkTools.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/19.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit
import AFNetworking

enum HJRequestMethod: String{
    case Post = "post"
    case Get = "get"
}
class HJNetworkTools: AFHTTPSessionManager {
    //单例
    static let shareTools:HJNetworkTools = {
        let tools = HJNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    // 定义请求
    typealias HMRequestCallBack = (responseObject: AnyObject?, error: NSError?)->()
    
    
    // 如果以后在公司开发过程中，某种类型的请求方式使用的频率比较高，那么可以给请求参数添加一个默认值
    /// 发送请求数据
    ///
    /// - parameter method:     请求方式
    /// - parameter urlString:  请求地址
    /// - parameter parameters: 请求参数
    /// - parameter finished:   请求完成之后的回调
    func request(method: HJRequestMethod = .Get, urlString: String, parameters: AnyObject?, finished: HMRequestCallBack){
        
        // 定义一个请求成功之后要执行的闭包
        let success = { (dataTask: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
            // 请求成功的回调
            finished(responseObject: responseObject, error: nil)
        }
        
        // 定义一个请求失败之后要执行的闭包
        let failure = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            finished(responseObject: nil, error: error)
        }
        
        if method == .Get {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    func upload(urlString: String, parameters: AnyObject?, datas: [String: NSData], finished: HMRequestCallBack) {
        
        self.POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            // 拼装要上传的数据
            for (key, value) in datas {
                formData.appendPartWithFileData(value, name: key, fileName: "aa.png", mimeType: "application/octet-stream")
            }
            }, progress: nil, success: { (_, responseObject) -> Void in
                // 请求成功的回调
                finished(responseObject: responseObject, error: nil)
            }) { (_, error) -> Void in
                finished(responseObject: nil, error: error)
        }
    }
    
//    func request(method:HJRequestMethod, urlString: String, parameters: AnyObject?, finish:((responseObject: AnyObject?, error: NSError?) -> ())){
//        if method == .Post{
//            POST(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) -> Void in
//                finish(responseObject:responseObject , error: nil)
//                }, failure: { (_, error) -> Void in
//                    finish(responseObject: nil, error: error)
//            })
//        }else{
//            GET(urlString, parameters: parameters, progress: nil, success: { (_, responseObject) -> Void in
//                finish(responseObject: responseObject, error: nil)
//                }, failure: { (_, error) -> Void in
//                    finish(responseObject: nil, error: error)
//            })
//        }
//    }
}
